// API服务层
import axios from 'axios';
import type { Dataset, Entity, FeatureView, Job, Model, ApiResponse } from '../types';

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 这里可以添加认证token等
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.error('API错误:', error);
    return Promise.reject(error);
  }
);

// API接口定义
export const datasetApi = {
  // 获取所有数据集
  getAll: (): Promise<ApiResponse<Dataset[]>> => {
    return api.get('/datasets');
  },
  // 上传数据集
  upload: (formData: FormData): Promise<ApiResponse<Dataset>> => {
    return api.post('/datasets/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },
  // 获取数据集详情
  getById: (id: string): Promise<ApiResponse<Dataset>> => {
    return api.get(`/datasets/${id}`);
  },
  // 删除数据集
  delete: (id: string): Promise<ApiResponse<void>> => {
    return api.delete(`/datasets/${id}`);
  }
};

export const entityApi = {
  // 获取所有实体
  getAll: (): Promise<ApiResponse<Entity[]>> => {
    return api.get('/entities');
  },
  // 创建实体
  create: (entity: Omit<Entity, 'id'>): Promise<ApiResponse<Entity>> => {
    return api.post('/entities', entity);
  },
  // 获取实体详情
  getById: (id: string): Promise<ApiResponse<Entity>> => {
    return api.get(`/entities/${id}`);
  },
  // 更新实体
  update: (id: string, entity: Partial<Entity>): Promise<ApiResponse<Entity>> => {
    return api.put(`/entities/${id}`, entity);
  },
  // 删除实体
  delete: (id: string): Promise<ApiResponse<void>> => {
    return api.delete(`/entities/${id}`);
  }
};

export const featureViewApi = {
  // 获取所有特征视图
  getAll: (): Promise<ApiResponse<FeatureView[]>> => {
    return api.get('/feature-views');
  },
  // 创建特征视图
  create: (featureView: Omit<FeatureView, 'id' | 'createdAt'>): Promise<ApiResponse<FeatureView>> => {
    return api.post('/feature-views', featureView);
  },
  // 获取特征视图详情
  getById: (id: string): Promise<ApiResponse<FeatureView>> => {
    return api.get(`/feature-views/${id}`);
  },
  // 删除特征视图
  delete: (id: string): Promise<ApiResponse<void>> => {
    return api.delete(`/feature-views/${id}`);
  }
};

export const jobApi = {
  // 获取所有作业
  getAll: (): Promise<ApiResponse<Job[]>> => {
    return api.get('/jobs');
  },
  // 创建作业
  create: (job: Omit<Job, 'id' | 'status' | 'createdAt' | 'updatedAt'>): Promise<ApiResponse<Job>> => {
    return api.post('/jobs', job);
  },
  // 获取作业详情
  getById: (id: string): Promise<ApiResponse<Job>> => {
    return api.get(`/jobs/${id}`);
  },
  // 取消作业
  cancel: (id: string): Promise<ApiResponse<void>> => {
    return api.post(`/jobs/${id}/cancel`);
  },
  // 获取作业状态
  getStatus: (id: string): Promise<ApiResponse<{ status: Job['status'] }>> => {
    return api.get(`/jobs/${id}/status`);
  }
};

export const modelApi = {
  // 获取所有模型
  getAll: (): Promise<ApiResponse<Model[]>> => {
    return api.get('/models');
  },
  // 保存模型
  save: (modelData: Omit<Model, 'id' | 'createdAt' | 'version'>): Promise<ApiResponse<Model>> => {
    return api.post('/models', modelData);
  },
  // 获取模型详情
  getById: (id: string): Promise<ApiResponse<Model>> => {
    return api.get(`/models/${id}`);
  },
  // 删除模型
  delete: (id: string): Promise<ApiResponse<void>> => {
    return api.delete(`/models/${id}`);
  }
};

export default api;